/**
* Copyright (c) 2022 - present TinyVue Authors.
* Copyright (c) 2022 - present Huawei Cloud Computing Technologies Co., Ltd.
*
* Use of this source code is governed by an MIT-style license.
*
* THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
* BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR
* A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS.
*
*/

@import '../custom.less';
@import '../mixins/common.less';
@import './vars.less';

@form-prefix-cls: ~'@{css-prefix}form';
@form-item-prefix-cls: ~'@{css-prefix}form-item';
@input-prefix-cls: ~'@{css-prefix}input';
@input-group-prefix-cls: ~'@{css-prefix}input-group';
@textarea-prefix-cls: ~'@{css-prefix}textarea';
@button-prefix-cls: ~'@{css-prefix}button';
@numeric-prefix-cls: ~'@{css-prefix}numeric';
@cascader-prefix-cls: ~'@{css-prefix}cascader';
@autocomplete-prefix-cls: ~'@{css-prefix}autocomplete';
@date-editor-prefix-cls: ~'@{css-prefix}date-editor';
@tooltip-prefix-cls: ~'@{css-prefix}tooltip';
@switch-prefix-cls: ~'@{css-prefix}switch';
@range-editor-prefix-cls: ~'@{css-prefix}range-editor';
@select-prefix-cls: ~'@{css-prefix}select';
@rate-prefix-cls: ~'@{css-prefix}rate';

.size-height(@height, @margin-bottom) {
  margin-bottom: @margin-bottom;

  .@{form-item-prefix-cls}__content {
    line-height: @height;

    .@{css-prefix}checkbox,
    .@{css-prefix}radio {
      line-height: @height;
    }
    .@{css-prefix}checkbox-group {
      min-height: @height;
    }
    .@{numeric-prefix-cls} {
      display: block;

      &__input-inner {
        height: @height;
      }
    }
    .@{css-prefix}slider-container {
      height: @height;
    }
  }

  .@{form-item-prefix-cls}__label {
    height: @height;
    line-height: @height;
  }
}

.star-selector(@content) {
  &.is-required:not(.is-no-asterisk) {
    // 子选择器是避免影响嵌套表单场景
    .@{form-item-prefix-cls}__label-wrap > .@{form-item-prefix-cls}__label:before,
    & > .@{form-item-prefix-cls}__label:before {
      @content();
    }
  }
}

.@{form-item-prefix-cls} {
  .inject-FormItem-vars();

  .clearfix();

  & & {
    margin-bottom: 0;
  }

  &--mini &__error {
    padding-top: 2px;
  }

  .size-height(
    var(--tv-FormItem-height),
    var(--tv-FormItem-margin-bottom)
  );

  &--medium {
    .size-height(
      var(--tv-FormItem-height-lg),
      var(--tv-FormItem-margin-bottom-lg)
    );
  }

  &--small {
    .size-height(
      var(--tv-FormItem-height-sm),
      var(--tv-FormItem-margin-bottom-sm)
    );
  }

  &--mini {
    .size-height(
      var(--tv-FormItem-height-xs),
      var(--tv-FormItem-margin-bottom-xs)
    );
  }

  // label-width设置为auto时样式
  &__label-wrap {
    float: left;

    .@{form-item-prefix-cls}__label {
      display: inline-block;
      float: none;
    }
  }

  &__label {
    text-align: right;
    vertical-align: middle;
    float: left;
    font-size: var(--tv-FormItem-font-size);
    color: var(--tv-FormItem-text-color);
    padding-right: var(--tv-FormItem-label-padding-right);
    box-sizing: border-box;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }

  &__content {
    position: relative;
    font-size: var(--tv-FormItem-font-size);
    .clearfix();

    .@{input-prefix-cls},
    .@{rate-prefix-cls},
    .@{select-prefix-cls},
    .@{css-prefix}checkbox,
    .@{css-prefix}radio,
    .@{css-prefix}link,
    .@{css-prefix}numeric {
      vertical-align: bottom;

      &.is-display-only {
        vertical-align: middle;
        line-height: 1.5;
      }
    }

    .@{input-prefix-cls} {
      display: inline-block;
      &.@{range-editor-prefix-cls}.@{input-prefix-cls}__inner {
        display: inline-flex;
      }

      &.@{input-group-prefix-cls} {
        display: inline-table;
      }
    }

    .@{rate-prefix-cls} {
      &__text {
        line-height: var(--tv-FormItem-line-height);
      }
    }

    .@{input-group-prefix-cls} {
      vertical-align: top;
    }

    .@{button-prefix-cls} + .@{button-prefix-cls} {
      margin-left: 8px;
    }
    .@{css-prefix}checkbox-group {
      &:has(> label.is-group-display-only) {
        min-height: unset;
        vertical-align: baseline;
        display: inline-block;
      }
    }

    .@{numeric-prefix-cls},
    .@{cascader-prefix-cls},
    .@{autocomplete-prefix-cls},
    .@{date-editor-prefix-cls}--daterange.@{input-prefix-cls},
    .@{date-editor-prefix-cls}--daterange.@{input-prefix-cls}__inner,
    .@{date-editor-prefix-cls}--timerange.@{input-prefix-cls},
    .@{date-editor-prefix-cls}--timerange.@{input-prefix-cls}__inner {
      width: 100%;
    }
  }

  &__error {
    color: var(--tv-FormItem-text-color-error);
    font-size: var(--tv-FormItem-font-size);
    line-height: 1;
    padding-top: 4px;
    position: absolute;

    .validate-icon {
      fill: var(--tv-FormItem-icon-color-error);
      margin-right: 4px;
      flex-shrink: 0;
    }
    .@{form-item-prefix-cls}__validate-message {
      vertical-align: middle;
    }

    &--inline {
      position: relative;
      display: inline-block;
      margin-left: 8px;
    }

    &--block {
      position: relative;
      display: flex;
      margin-left: 0;
      padding-top: var(--tv-FormItem-error-block-padding-top);
    }
  }

  .star-selector({
    content: '*';
    color: var(--tv-FormItem-text-color-error);
    margin-right: 4px;
  });

  &.is-error {
    .@{input-prefix-cls}__inner,
    .@{input-prefix-cls}__inner:focus,
    .@{textarea-prefix-cls},
    .@{textarea-prefix-cls}::before,
    .@{textarea-prefix-cls}::after,
    .@{textarea-prefix-cls}:focus,
    .@{textarea-prefix-cls}__inner,
    .@{textarea-prefix-cls}__inner:focus {
      border-color: var(--tv-FormItem-border-color-error);
      background-color: var(--tv-FormItem-bg-color-error);
    }

    .@{input-group-prefix-cls}__append,
    .@{input-group-prefix-cls}__prepend {
      .@{input-prefix-cls}__inner {
        border-color: transparent;
      }
    }

    .@{numeric-prefix-cls}__input-inner,
    .@{numeric-prefix-cls}__input-inner:focus {
      border-color: var(--tv-FormItem-border-color-error);
      background-color: var(--tv-FormItem-bg-color-error);
    }
  }

  &__extra-tip {
    line-height: var(--tv-FormItem-line-height);
    margin-top: 8px;
    color: var(--tv-FormItem-text-color-weaken);
    font-size: var(--tv-FormItem-font-size);
  }
}

.@{form-prefix-cls} {
  .inject-FormItem-vars();

  &--inline {
    .@{form-item-prefix-cls} {
      display: inline-block;
      vertical-align: top;
      margin-right: var(--tv-FormItem-inline-margin-right);
    }

    .@{form-item-prefix-cls}__label {
      float: none;
      display: inline-block;
    }

    .@{form-item-prefix-cls}__content {
      display: inline-block;
      vertical-align: top;
    }

    // 嵌套表单场景
    .@{form-prefix-cls}--label-top .@{form-item-prefix-cls}__content {
      display: block;
    }
  }

  &--label-left {
    &.has-required {
      .@{form-item-prefix-cls}__label {
        padding-left: 12px;
      }
    }
    .@{form-item-prefix-cls}__label {
      text-align: left;
    }

    &.label-align {
      .@{form-item-prefix-cls} {
        &.is-required {
          .@{form-item-prefix-cls}__label {
            padding-left: 12px;
            position: relative;

            &::before {
              position: absolute;
              left: 0;
            }
          }
          & ~ .@{form-item-prefix-cls} {
            .@{form-item-prefix-cls}__label {
              padding-left: 12px;
            }
          }
        }
      }
    }
  }

  &--label-top {
    .@{form-item-prefix-cls} {
      margin-bottom: 16px;
    }
    .@{form-item-prefix-cls}__label {
      float: none;
      display: block;
      text-align: left;
      padding: 0 0 8px;
      height: auto;
      line-height: var(--tv-FormItem-line-height);
    }
  }

  &.is-display-only {
    .@{form-item-prefix-cls}__label {
      line-height: var(--tv-FormItem-line-height);
      height: auto;
      vertical-align: inherit;
      color: var(--tv-FormItem-text-color-weaken);
    }

    .@{form-item-prefix-cls}__content {
      line-height: var(--tv-FormItem-line-height);
      font-size: var(--tv-FormItem-font-size);
      height: auto;
      vertical-align: inherit;

      // 一些组件的只读态实现方式怪异，需要一些修正样式来修复水平对齐问题
      .@{switch-prefix-cls} {
        margin-top: 0;
      }

      .@{css-prefix}numeric.is-display-only {
        margin-top: 2px;
      }
      .@{input-prefix-cls} {
        &.@{range-editor-prefix-cls}.@{input-prefix-cls}__inner {
          height: 18px;
        }
      }
    }

    .@{form-item-prefix-cls} {
      .star-selector({
        content: '';
      });
    }
  }
}
